﻿/*
 * Twipler - Twitter Web Client with features.
 *  
 *  Copyright (C) 2009, 2010. Ian Quigley
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details. 
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 */

using System;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using Twipler.App;
using Twipler.App.Session;
using Twipler.Data.Access;
using Twipler.Data.Entity.Twipler;
using Twipler.WebApp;

using NHibernate.Criterion;

namespace Settings.Admin
{
    [RequireUser(VisitorRole.SuperAdmin)]
    public partial class InternalSettings : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                UpdateDisplay();
        }

        private void UpdateDisplay()
        {
            grdSetting.DataSource = Factory.Setting.GetByCriteria(Expression.Eq("SettingType", (int)SettingType.ApplicationInternal));
            grdSetting.DataBind();

            StringBuilder html = new StringBuilder();

            html.AppendFormat(htmlTableBegin);
            html.AppendFormat(htmlTableRow, "Current Offset:", Twipler.App.Current.UtcSecondsOffsetLowest.TotalSeconds);
            html.AppendFormat(htmlTableRow, "Active sessions:", InnerSessionFactory.GetSessionCount());
            html.AppendFormat(htmlTableEnd);

            litReport.Text = html.ToString();
        }

        protected void editSetting_Update()
        {
            UpdateDisplay();
        }

        protected void grdSetting_ItemCommand(object source, DataGridCommandEventArgs e)
        {
            int settingId = int.Parse(e.Item.Cells[0].Text);

            switch (e.CommandName)
            {
                
                case "Edit":
                    editSetting.Entity = Factory.Setting.GetById(settingId, false);
                    editSetting.UpdateDisplay();
                    break;

                case "Delete":
                    Factory.Setting.Delete(Factory.Setting.GetById(settingId, false));
                    UpdateDisplay();
                    break;
            }

        }

        private const string htmlTableBegin = "<div class=\"formBox\"><h4>Internals</h4><table cellpadding=0 cellspacing=0 border=0 width=\"100%\">";
        private const string htmlTableRow = "<tr><td class=\"lbl\" align=\"right\">{0}</td><td><div style=\"margin: 2px;; background: #fff;\">{1}</div></td></tr>";
        private const string htmlTableEnd = "</table></div>";


    }
}